package com.baichang.taoshunda.service.impl;


import com.alipay.api.AppPay;
import com.baichang.framework.model.Page;
import com.baichang.framework.utils.KidUtils;
import com.baichang.taoshunda.dao.GoodsDao;
import com.baichang.taoshunda.dao.IncidentalDao;
import com.baichang.taoshunda.dao.TrendsUserDao;
import com.baichang.taoshunda.entity.Incidental;
import com.baichang.taoshunda.service.IncidentalService;
import com.baichang.taoshunda.service.ParamsService;
import com.baichang.taoshunda.service.TrendService;
import com.weixin.pay.api.WeixinPay;
import com.weixin.pay.api.model.PayInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2018/3/22.
 */
@Service
public class IncidentalServiceImpl implements IncidentalService {
    @Autowired
    private IncidentalDao incidentalDao;
    @Autowired
    private ParamsService paramsService;
    @Autowired
    private GoodsDao goodsDao;
    @Autowired
    private TrendsUserDao trendsUserDao;

    @Autowired
    private TrendService trendService;

    @Override
    public boolean addIncidental(Integer busId, Integer type, double money, String goodsId, Integer payMethod) {
        Incidental incidental = new Incidental();
        incidental.setAllId(goodsId);
        incidental.setCreated(new Date());
        incidental.setMoney(BigDecimal.valueOf(money));
        incidental.setType(type);
        incidental.setPayMethod(payMethod);
        if (type == 1) {
            goodsDao.updateGoodsStatus(goodsId, 1);
        } else {
//            trendsUserDao.updateTrendsStatus(goodsId, 1);
            trendService.updateStatusById(goodsId);
        }
        return incidentalDao.save(incidental) > 0;
    }

    @Override
    public Map<String, Object> payInfo(Integer typeId, int payMethod) {
        BigDecimal totalMoney;
        Map getValue;
        if (typeId == 1) {
            getValue = paramsService.getParamsValue(38, 37);
        } else {
            getValue = paramsService.getParamsValue(38, 38);
        }


        totalMoney = BigDecimal.valueOf(Double.parseDouble(getValue.get("value").toString()));
        String title = "商家服务费总付款：" + totalMoney + "元";
        String payOrderKid = "payOrder" + KidUtils.getKid();
        Map map = new HashMap();
        if (payMethod == 1) {
            //支付宝
            String aplipay = AppPay.createPay(title, payOrderKid, title, totalMoney.doubleValue());//这里传的是支付订单编号
            map.put("pay", aplipay);
        } else if (payMethod == 2) {
            int price = totalMoney.multiply(BigDecimal.valueOf(100)).intValue();
            WeixinPay weixinPay = new com.weixin.pay.api.AppPay();
            PayInfo payInfo = new PayInfo();
            payInfo.setBody(title);
            payInfo.setTradeNo(payOrderKid);
            payInfo.setSubject(title);
            payInfo.setPrice(price);
            payInfo.loadWxConfig("wx48217cb07b14d413");
            weixinPay.setPayInfo(payInfo);
            map = weixinPay.createPay();
        }
        return map;
    }

    /**
     * @param type
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page queryVideo(Integer type, int nowPage, int pageSize) {
        return incidentalDao.queryVideo(type, nowPage, pageSize);
    }
}
